package com.bifrii.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bifrii.transferObjects.TopCommunity;

public class JDBCTopCommunityDAO implements ITopCommunityDAO {

	public void insert(TopCommunity c) {
Connection conn = ConnectionManager.getInstance().checkOut();
		
        String sql = "INSERT INTO TopCommunity ( oid, idCommunity, events, users) VALUES (?, ?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, c.getOid());
            stmt.setInt(2, c.getIdCommunity());
            stmt.setInt(3, c.getEvents());
            stmt.setInt(4, c.getUsers());

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }


	}


	public void delete(Integer idCommunity) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		
		String sql = "DELETE FROM TopCommunity WHERE (idCommunity = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idCommunity);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}

	
	public List<TopCommunity> selectTopCommunities(Integer top) {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<TopCommunity> searchResults = new ArrayList<TopCommunity>();
        ResultSet result = null;

        try {

            String sql = "SELECT * from TopCommunity order by events desc, users desc limit 0,?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, top);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                TopCommunity temp = new TopCommunity();
                temp.setOid(result.getString("oid"));
                temp.setIdCommunity(result.getInt("idCommunity"));
                temp.setEvents(result.getInt("events"));
                temp.setUsers(result.getInt("users"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

}
